package net.example.activity.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.oraycn.esbasic.adapters.CommonAdapter;
import com.oraycn.esbasic.helpers.FileHelper;
import com.oraycn.esbasic.models.FileType;
import com.oraycn.esbasic.views.CircleProgressBar;
import com.oraycn.esframeworkboost.networkdisk.FileDetail;

import net.example.model.event.FileEvent;
import net.example.ggtalk.R;

import java.text.SimpleDateFormat;
import java.util.List;
import android.os.Handler;

public class FileInfoAdapter extends CommonAdapter<FileDetail> {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    ListView mListView;
    Handler handler;
    public static final int DataSetChangeCompleted=1001;
    public FileInfoAdapter(Context context, List<FileDetail> datas, int item_layoutId)
    {
        super(context,datas,item_layoutId);
    }

    public void setListView(ListView listView)
    {
        this.mListView=listView;
    }
    public void setHandler(Handler handler){this.handler=handler;}

    @Override
    protected void setConvertView(View convertView, FileDetail fileDetail, int itemViewType) {
        FileInfoAdapter.ViewHolder holder;
        if(convertView.getTag()==null)
        {
            holder = new FileInfoAdapter.ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else {
            holder = (FileInfoAdapter.ViewHolder) convertView.getTag();
        }
        this.setFileTypeImg(holder, fileDetail.Name);
        holder.tv_fileName.setText(fileDetail.Name);
        holder.tv_createTime.setText(dateFormat.format(fileDetail.CreateTime));
        holder.tv_fileSize.setText(FileHelper.getFileLengthString(fileDetail.Size));
        switch (fileDetail.Status)
        {
            case Normal:
                holder.file_status.setVisibility(View.GONE);
                holder.progressBar.setVisibility(View.GONE);
                break;
            case Transfering:
//                holder.progressBar.setVisibility(View.VISIBLE);
//                holder.progressBar.setMax(total);
//                holder.progressBar.setProgress(transferSize);
                break;
            case Failed:
                holder.file_status.setImageResource(R.drawable.failed);
                holder.file_status.setVisibility(View.VISIBLE);
                holder.progressBar.setVisibility(View.GONE);
                break;
            case Cancelled:
                holder.file_status.setVisibility(View.GONE);
                holder.progressBar.setVisibility(View.GONE);
                break;
            case Completed:
                holder.file_status.setImageResource(R.drawable.complete);
                holder.file_status.setVisibility(View.VISIBLE);
                holder.progressBar.setVisibility(View.GONE);
                break;
        }
    }


    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        if(this.handler!=null)
        {
            this.handler.sendEmptyMessage(DataSetChangeCompleted);
        }
    }

    public void fileProcess(FileEvent event) {
        String projectID = event.ProjectId;
        int total =  (int)event.FileSize;
        int transferSize = event.TransferSize;
        View v = this.getConvertView(projectID);
        if (v != null) {
            FileInfoAdapter.ViewHolder holder = (FileInfoAdapter.ViewHolder) v.getTag();
            if (holder != null )
            {
                if(holder.file_status.getVisibility()==View.VISIBLE){holder.file_status.setVisibility(View.GONE);}
                holder.progressBar.setVisibility(View.VISIBLE);
                holder.progressBar.setMax(total);
                holder.progressBar.setProgress(transferSize);
            }
        }
    }

    private View getConvertView(String projectID)
    {
        int position = -1;
        for (int x = 0 ; x <dataList.size(); x++) {
            FileDetail msg = dataList.get(x);
            if (msg.Name != null && projectID.equals(msg.ProjectId)) {
                position = x;
                break;
            }
        }
        if(position==-1){return null;}
        int fpos = mListView.getFirstVisiblePosition();
        return mListView.getChildAt(position - fpos);
    }


    private void setFileTypeImg(FileInfoAdapter.ViewHolder holder, String fileName)
    {
        FileType type= FileHelper.getFileType(fileName);
        int resID=R.drawable.file_type_unknown;
        switch (type)
        {
            case Video:
                resID=R.drawable.file_type_video;
                break;
            case Music:
                resID=R.drawable.file_type_music;
                break;
            case Rar:
                resID=R.drawable.file_type_rar;
                break;
            case PPT:
                resID=R.drawable.file_type_ppt;
                break;
            case Word:
                resID=R.drawable.file_type_word;
                break;
            case Excel:
                resID=R.drawable.file_type_excel;
                break;
            case Txt:
                resID=R.drawable.file_type_txt;
                break;
            case PDF:
                resID=R.drawable.file_type_pdf;
                break;
            case Image:
                resID=R.drawable.file_type_image;
                break;
            case Other:
                resID=R.drawable.file_type_unknown;
        }
        holder.fileTypeImg.setImageResource(resID);//根据文件类型设置图像
    }

    private class ViewHolder
    {
        public ImageView fileTypeImg ,file_status;
        public TextView tv_fileName;
        public TextView tv_fileSize;
        public TextView tv_createTime;
        public CircleProgressBar progressBar;
        public ViewHolder(View view)
        {
            this.fileTypeImg = (ImageView) view.findViewById(R.id.file_type_img);
            this.tv_fileName =(TextView) view.findViewById(R.id.file_name);
            this.tv_createTime=(TextView) view.findViewById(R.id.time);
            this.tv_fileSize =(TextView) view.findViewById(R.id.file_size);
            this.progressBar=(CircleProgressBar)view.findViewById(R.id.progressBar);
            this.file_status=(ImageView)view.findViewById(R.id.file_status);
        }
    }
}
